package com.ruoyi.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.domain.*;
import com.ruoyi.order.mapper.CheEvaluateMapper;
import com.ruoyi.order.mapper.CheOrderMapper;
import com.ruoyi.order.mapper.CheServiceMapper;
import com.ruoyi.order.service.ICheOrderAndUserService;
import com.ruoyi.shop.api.CheApiShopService;
import com.ruoyi.user.api.CheApiUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @ClassName CheOrderAndUserServiceImpl
 * @Description TODO
 * @Author liuzhiqiang
 * @Date 2024年06月05日 14:45
 * @Version 1.0
 */
@Service
public class CheOrderAndUserServiceImpl implements ICheOrderAndUserService  {
    @Autowired
    private CheApiUserService cheUserService;
    @Autowired
    private CheApiShopService cheApiShopService;
    @Autowired
    private CheOrderMapper cheOrderMapper;
    @Autowired
    private CheServiceMapper cheServiceMapper;
    @Autowired
    private CheEvaluateMapper cheEvaluateMapper;
    @Override
    public List<CheOrder1> selectCheOrderList(CheOrder cheOrder) {
        List<CheOrder1> list =new ArrayList<>();
        List<CheOrder> cheOrders = cheOrderMapper.selectCheOrderList(cheOrder);
        for(CheOrder ch:cheOrders){
            CheOrder1 che1=new CheOrder1();
            Long userId = ch.getUserId();
            AjaxResult info = cheApiShopService.getInfo(ch.getUserId(), SecurityConstants.INNER);
            AjaxResult result = cheUserService.getInfo(userId, SecurityConstants.INNER);
            //商家
            LinkedHashMap bb=(LinkedHashMap)info.get("data");
            CheShop data1 = BeanUtil.mapToBean(bb, CheShop.class, true);
            //用户
            LinkedHashMap aa = (LinkedHashMap)result.get("data");
            CheUser data = BeanUtil.mapToBean(aa, CheUser.class, true);

            che1.setId(ch.getId());
            //商家
            che1.setShopName(data1.getShopName());
            che1.setNumber(ch.getNumber());
            //用户
            String phone = data.getPhone();
            che1.setUserName(data.getRealName()+"<br>"+phone);
            //服务项目
            List<CheServiceName> cheServiceNames = cheServiceMapper.selectCheOrderById(ch.getId());
            String str="项目:";
            //现价
            Double ne=0.00;
            for (CheServiceName ser:cheServiceNames){
                String name = ser.getName();
                Double newPrice = ser.getNewPrice();
                str+=name+";";
                ne+=newPrice;
            }
            //服务名称
            che1.setServiceName(str);
            //实付
            che1.setRealPrice(ch.getRealPrice());
            //优惠
            BigDecimal big = BigDecimal.valueOf(ne);
            BigDecimal realPrice = ch.getRealPrice();
            BigDecimal coupon = big.subtract(realPrice);
            che1.setCoupon(coupon);
            //金额传输
            String a = big.toString();
            String b = realPrice.toString();
            String c = coupon.toString();
            String abc="原价:$"+a+"<br>"+"优惠:$"+c+"<br>"+"实付:$"+b;
            che1.setAbc(abc);
            //状态
            String state=null;
            if(ch.getState().equals("0")){
                state="待支付";
            }
            if(ch.getState().equals("1")){
                state="待使用";
            }
            if(ch.getState().equals("2")){
                state="待评价";
            }
            if(ch.getState().equals("3")){
                state="已取消";
            }
            if(ch.getState().equals("4")){
                state="已退款";
            }
            if(ch.getState().equals("5")){
                state="已评价";
            }
            che1.setState(state);

            //预约时间
            che1.setSubscribeTime(ch.getSubscribeTime());
            //下单时间
            che1.setCreateTime(ch.getCreateTime());
            list.add(che1);
        }
        return list;
    }

    @Override
    public List<CheOrder1> selectCheOrderList2(CheOrder cheOrder) {
        List<CheOrder1> list =new ArrayList<>();
        List<CheOrder> cheOrders = cheOrderMapper.selectCheOrderList2(cheOrder);
        for(CheOrder ch:cheOrders){
            CheOrder1 che1=new CheOrder1();
            Long userId = ch.getUserId();
            AjaxResult info = cheApiShopService.getInfo(ch.getUserId(), SecurityConstants.INNER);
            AjaxResult result = cheUserService.getInfo(userId, SecurityConstants.INNER);
            //商家
            LinkedHashMap bb=(LinkedHashMap)info.get("data");
            CheShop data1 = BeanUtil.mapToBean(bb, CheShop.class, true);
            //用户
            LinkedHashMap aa = (LinkedHashMap)result.get("data");
            CheUser data = BeanUtil.mapToBean(aa, CheUser.class, true);

            che1.setId(ch.getId());
            //商家
            che1.setShopName(data1.getShopName());
            che1.setNumber(ch.getNumber());
            //用户
            String phone = data.getPhone();
            che1.setUserName(data.getRealName()+"<br>"+phone);

            //服务项目
            List<CheServiceName> cheServiceNames = cheServiceMapper.selectCheOrderById(ch.getId());
            String str="项目:";
            //现价
            Double ne=0.00;
            for (CheServiceName ser:cheServiceNames){
                String name = ser.getName();
                Double newPrice = ser.getNewPrice();
                str+=name+";";
                ne+=newPrice;
            }
            //服务名称
            che1.setServiceName(str);

            //实付
            che1.setRealPrice(ch.getRealPrice());
            //优惠
            BigDecimal big = BigDecimal.valueOf(ne);
            BigDecimal realPrice = ch.getRealPrice();
            BigDecimal coupon = big.subtract(realPrice);
            che1.setCoupon(coupon);
            //金额传输
            String a = big.toString();
            String b = realPrice.toString();
            String c = coupon.toString();
            String abc="原价:$"+a+"<br>"+"优惠:$"+c+"<br>"+"实付:$"+b;
            che1.setAbc(abc);

            //状态
            String state=null;
            if(ch.getState().equals("0")){
                state="待支付";
            }
            if(ch.getState().equals("1")){
                state="待使用";
            }
            if(ch.getState().equals("2")){
                state="待评价";
            }
            if(ch.getState().equals("3")){
                state="已取消";
            }
            if(ch.getState().equals("4")){
                state="已退款";
            }
            if(ch.getState().equals("5")){
                state="已评价";
            }
            che1.setState(state);

            //预约时间
            che1.setSubscribeTime(ch.getSubscribeTime());
            //核销码
            che1.setVerificationCode(ch.getVerificationCode());
            //下单时间
            che1.setCreateTime(ch.getCreateTime());

            list.add(che1);


        }
        return list;
    }

    @Override
    public List<CheOrder1> selectCheOrderList3(CheOrder cheOrder) {
        List<CheOrder1> list =new ArrayList<>();
        List<CheOrder> cheOrders = cheOrderMapper.selectCheOrderList3(cheOrder);
        for(CheOrder ch:cheOrders){
            CheOrder1 che1=new CheOrder1();
            Long userId = ch.getUserId();
            AjaxResult info = cheApiShopService.getInfo(ch.getUserId(), SecurityConstants.INNER);
            AjaxResult result = cheUserService.getInfo(userId, SecurityConstants.INNER);
            //商家
            LinkedHashMap bb=(LinkedHashMap)info.get("data");
            CheShop data1 = BeanUtil.mapToBean(bb, CheShop.class, true);
            //用户
            LinkedHashMap aa = (LinkedHashMap)result.get("data");
            CheUser data = BeanUtil.mapToBean(aa, CheUser.class, true);

            che1.setId(ch.getId());
            //商家
            che1.setShopName(data1.getShopName());
            che1.setNumber(ch.getNumber());
            //用户
            String phone = data.getPhone();
            che1.setUserName(data.getRealName()+"<br>"+phone);
            //服务项目
            List<CheServiceName> cheServiceNames = cheServiceMapper.selectCheOrderById(ch.getId());
            String str="项目:";
            //现价
            Double ne=0.00;
            for (CheServiceName ser:cheServiceNames){
                String name = ser.getName();
                Double newPrice = ser.getNewPrice();
                str+=name+";";
                ne+=newPrice;
            }
            //服务名称
            che1.setServiceName(str);
            //实付
            che1.setRealPrice(ch.getRealPrice());
            //优惠
            BigDecimal big = BigDecimal.valueOf(ne);
            BigDecimal realPrice = ch.getRealPrice();
            BigDecimal coupon = big.subtract(realPrice);
            che1.setCoupon(coupon);
            //金额传输
            String a = big.toString();
            String b = realPrice.toString();
            String c = coupon.toString();
            String abc="原价:$"+a+"<br>"+"优惠:$"+c+"<br>"+"实付:$"+b;
            che1.setAbc(abc);
            //状态
            String state=null;
            if(ch.getState().equals("0")){
                state="待支付";
            }
            if(ch.getState().equals("1")){
                state="待使用";
            }
            if(ch.getState().equals("2")){
                state="待评价";
            }
            if(ch.getState().equals("3")){
                state="已取消";
            }
            if(ch.getState().equals("4")){
                state="已退款";
            }
            if(ch.getState().equals("5")){
                state="已评价";
            }
            che1.setState(state);

            //预约时间
            che1.setSubscribeTime(ch.getSubscribeTime());
            //下单时间
            che1.setCreateTime(ch.getCreateTime());
            list.add(che1);
        }
        return list;
    }

    @Override
    public List<CheEvaluate> selectCheOrderList4(CheEvaluate cheEvaluate) {
        List<CheEvaluate> list = cheEvaluateMapper.list(cheEvaluate);
        for(CheEvaluate che:list){
            Long userId = che.getUserId();
            List<CheServiceName> cheServiceNames = cheServiceMapper.selectCheOrderById(userId);
            String src[]={"1", "2"};
            ArrayList<String> arr = new ArrayList<>(Arrays.asList(src));
            arr.clear();
            for (CheServiceName ser:cheServiceNames){
                String name = ser.getName();
                arr.add(name);
            }
            String[] strings = arr.toArray(new String[0]);
            //服务名称
            che.setService(strings);
            AjaxResult info = cheUserService.getInfo(userId, SecurityConstants.INNER);
            LinkedHashMap aa = (LinkedHashMap)info.get("data");
            CheUser data = BeanUtil.mapToBean(aa, CheUser.class, true);
            String s= data.getRealName();
            String p= data.getPhone();
            //用户和手机号
            che.setUserName(s+p);
            List<String> list1 = cheEvaluateMapper.list1(che.getOrderIdw());
            //图片名称
            String[] s1 = list1.toArray(new String[0]);
            che.setImages(s1);
        }
        return list;
    }


}
